#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Node {
    int data;
    struct Node *pre;
    struct Node *next;
} Node;

Node *initList() 
{
    Node *list = (Node *)malloc(sizeof(Node));
    list->data = 0;
    list->pre = NULL;
    list->next = NULL;
    return list;
}

void headInsert(Node *list, int data)
{
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    if (list->data == 0) {
        node->pre = list;
        node->next = NULL; //即list->next
        list->next = node;
    } else {
        node->next = list->next;  //先改后面的 
        list->next->pre = node;   //因为链表只能向后遍历
        list->next = node;
        node->pre = list; 
    }
    list->data++;
}

void tailInsert(Node *list, int data) 
{
    Node *tmp = list;
    Node *node = (Node *)malloc(sizeof(Node)); 
    node->data = data;
    while (tmp->next) {
        tmp = tmp->next;
    }
    node->pre = tmp;
    node->next = NULL;//即tmp->next
    tmp->next = node;
    list->data++;
}

void printList(Node *list)
{
    Node *tmp = list = list->next;
    while (tmp) {
        printf("%d->", tmp->data);
        tmp = tmp->next;
    }
    printf("NULL\n");
}

int delete(Node *list, int data) 
{
    Node *tmp = list->next;
    while (tmp) {
        if (tmp->data == data) {
            tmp->next->pre = tmp->pre;
            tmp->pre->next = tmp->next;
            free(tmp);
            return true;
        }
        tmp = tmp->next;
    }
    return false;
}

int main(void) 
{
    Node *list = initList();
    tailInsert(list, 5);
    tailInsert(list, 6);
    headInsert(list, 1);
    headInsert(list, 2);
    headInsert(list, 3);
    headInsert(list, 4);
    
   
    printList(list);

    return 0;
}